package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.PartsWarn;
import com.sl.au.service.PartsWarnService;


@RestController
@RequestMapping(value = "/ui/partsWarn")
public class PartsWarnController extends BaseController {

	
	@Autowired
	PartsWarnService partsWarnService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("partsWarn/partsWarn");
		return modelAndView;
	}

	/**
	 * @param request
	 * @return 得到选项配置PartsWarn的map，关键字为key
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String id =request.getParameter("id");
		String key =request.getParameter("key");
		if(id == null) id = "";
		if(key == null) key = "";
		List<PartsWarn> partsWarns = null;
		partsWarns = partsWarnService.getPage(start, length, id , key);
		Long count = null;
		count = partsWarnService.getCount(id , key);
		map.put("data", partsWarns);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	///保存
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> savePartsWarn(HttpServletRequest request, @RequestBody PartsWarn partsWarn) {
		Map<String, Object> map = new HashMap<String, Object>();
		partsWarnService.getRepository().save(partsWarn);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * @param request
	 * @return 得到选项配置PartsAttributes的map，关键字为id
	 */
	@RequestMapping(value = "getAllWarnById", method = RequestMethod.GET)
	public Map<String, Object> getAttributesByTypeid(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(id == null) return map;
		List<PartsWarn> partsWarns = null;
		partsWarns = partsWarnService.getAllWarnById(id);
		map.put("data", partsWarns);
		return map;
	}
}
